package top.laoshuzi.cloudplatform.tools;

import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Mouse on 2017/5/5.
 */

public class FileHelper {

    private static FileHelper instance = null;

    private File picFileDir;
    private File mvFileDir;

    private FileHelper() {
        this.picFileDir = FileUtil.getPicFileDir();
        this.mvFileDir = FileUtil.getMvFileDir();
    }

    public static FileHelper getInstance() {
        if (instance == null) {
            synchronized (FileHelper.class) {
                if (instance == null) {
                    instance = new FileHelper();
                }
            }
        }
        return instance;
    }

    public List<File> getAllPicFiles() {

        File[] files = picFileDir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                String fileName = pathname.getName();
                String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
                return suffix.equals("jpg");
            }
        });

        return Arrays.asList(files);
    }

    public List<File> getPicFiles(final int count) {

        File[] files = picFileDir.listFiles(new FileFilter() {

            int i = count;

            @Override
            public boolean accept(File pathname) {
                if (i-- > 0) {
                    return true;
                }
                return false;
            }
        });
        return Arrays.asList(files);
    }

    public List<File> getAllMvFiles() {

        File[] files = mvFileDir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                String fileName = pathname.getName();
                String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
                return suffix.equals("mp4");
            }
        });

        return Arrays.asList(files);
    }

    public List<File> getMvFiles(final int count) {

        File[] files = mvFileDir.listFiles(new FileFilter() {

            int i = count;

            @Override
            public boolean accept(File pathname) {
                if (i-- > 0) {
                    return true;
                }
                return false;
            }
        });
        return Arrays.asList(files);
    }

}
